/*
 * package core: Browser, Dom, Ajax, Json, Animation
 */

if(typeof jav == 'undefined' || !jav){
	var jav = {};
}
jav.namespase = function(){
	var a = arguments, o = null, i, j, d;
	for(i=0; i<a.length; ++i){
		d = a[i].split('.');
		o = jav;
		for(j=(d[0]=='jav') ? 1 : 0; j<d.length; ++j){
			o[d[j]] = o[d[j]] || {};
			o = o[d[j]];
		}
	}
	return o;
};

(function(){
	var D = jav.dom, 
		reClassNameCache={},
		
		//dom aliases
		document = window.document,
		documentElement = window.documentElement,
		
		//string constants
		OWNER_DOCUMENT = 'ownerDocument',
		COMPAT_MODE = 'compatMode',
		OFFSET_LEFT = 'offsetLeft',
        OFFSET_TOP = 'offsetTop',
		CSS1_COMPAT = 'CSS1Compat',
        BACK_COMPAT = 'BackCompat',
		DOCUMENT_ELEMENT = 'documentElement',
		GET_BOUNDING_CLIENT_RECT = 'getBoundingClientRect',
		LEFT = 'left',
        TOP = 'top',
		GET_COMPUTED_STYLE = 'getComputedStyle',
		CURRENT_STYLE = 'currentStyle',
		BORDER_LEFT_WIDTH = 'borderLeftWidth',
	    BORDER_TOP_WIDTH = 'borderTopWidth',
	    NODE_TYPE = 'nodeType';
	
	var getClassRegEx = function(className){
		var re = reClassNameCache[className];
		if(!re){
			re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
			reClassNameCache[className] = re;
		}
		return re;
	}
	
/**
 * javascript language
 */
	jav.lang = {
		trim : function(s){
			try{
				return s.replace(/^\s+|s+$/g, '');
			}catch(e){
				return s;
			}
		}
	};
/**
 * event util
 */	
	jav.ev = {
		getTarget : function(ev, resolveTextNode){
			var oTag = ev.target || ev.srcElement;
			return this.resolveTextNode(oTag);
		},
		resolveTextNode : function(oTag){
			try{
				if(oTag && oTag.nodeType == 3){
					return oTag.parentNode;
				}
			}catch(e){}
			return oTag;
		},
		addEventHandler : function(oTag, sEType, fn){
			if(oTag.addEventListener){
				oTag.addEventListener(sEType, fn, false);
			}else if(oTag.attachEvent){
				oTag.attachEvent('on'+sEType, fn);
			}else{
				return function(){};
			}
		},
		removeEventHandler : function(){
			if(window.removeEventListener){
				return function(oTag, sEType, fn){
					oTag.removeEventListener(sEType, fn, false);
				};
			}else if(window.detachEvent){
				return function(oTag, sEType, fn){
					oTag.detachEvent('on'+sEType, fn);
				};
			}else{
				return function(){};
			}
		}()
	};
	
/**
 * dom util
 */	
	var _testElement = function(node){
		return node && node[NODE_TYPE] == 1;
	};
	jav.dom = {
		getId : function(el){
			if(el){
				if(el.nodeType || el.item){
					return el;
				}
				if(typeof el==='string'){
					return document.getElementById(el);
				}
				if('length' in el){
					var c = [];
					for(var i=0,len=el.length; i<len; i++){
						c[c.length]=jav.dom.getId(el[i]);
					}
					return c;
				}
				return el;
			}
			return null;
		},
		getClass : function(className, tag, root){
			className = jav.lang.trim(className);
			tag = tag || '*';
			root = (root) ? D.gId(root) : null || document;
			if(!root){
				return [];
			}
			
			var nodes = [], 
				elements=document.getElementsByTagName(tag),
				re = getClassRegEx(className);
			
			for(var i=0, len=elements.length; i<len; i++){
				if(re.test(elements[i].className)){
					nodes[nodes.length]=elements[i];
				}
			}
			return nodes;
		},
		hasClass : function(){
			
		},
		removeClass : function(){
			
		},
		setStyles : function(el, oStyle){
			
		},
		getXY : function(){
			if(document[DOCUMENT_ELEMENT][GET_BOUNDING_CLIENT_RECT]){
				return function(node){
					var scrollLeft, scrollTop, box, doc,
						off1, off2, mode, bLeft, bTop,
						floor = Math.floor,
						xy = false;
					
					box = node[GET_BOUNDING_CLIENT_RECT]();
					doc = node[OWNER_DOCUMENT];
					
					scrollLeft = this.getDocumentScrollLeft(doc);
					scrollTop = this.getDocumentScrollTop(doc);
					xy = [floor(box[LEFT]), floor(box[TOP])];
					
					if(scrollLeft || scrollTop){
						xy[0] += scrollLeft;
						xy[1] += scrollTop;
					}
					
					xy[0] = floor(xy[0]);
					xy[1] = floor(xy[1]);
					return xy;
				}
			}else{
				return function(node){
					var docScrollTop, docScrollLeft, doc,
						scrollTop, scrollLeft,
						floor = Match.floor,
						xy = false;
					
					doc = node[OWNER_DOCUMENT];
					
					docScrollTop = this.getDocumentScrollLeft(doc);
					docScrollLeft = this.getDocumentScrollTop(doc);
					xy = [node[OFFSET_LEFT], node[OFFSET_TOP]];
					
					if(scrollLeft || scrollTop){
						xy[0] += scrollLeft;
						xy[1] += scrollTop;
					}
					
					xy[0] = floor(xy[0]);
					xy[1] = floor(xy[1]);
					return xy;
				}
			}
		}(),
		getDocumentScrollLeft : function(doc){
			doc = doc || document;
			return Math.max(doc[DOCUMENT_ELEMENT].scrollLeft, doc.body.scrollLeft);
		},
		getDocumentScrollTop : function(doc){
			doc = doc || document;
			return Math.max(doc[DOCUMENT_ELEMENT].scrollTop, doc.body.scrollTop);
		},
		getChildren : function(el){
			el = this.getId(el);
			var children = el.childNodes, len = children.length, newChildren = [];
			for(var i=0; i<len; i++){
				if(_testElement(children[i])){
					newChildren[newChildren.length] = children[i];
				}
			}
			return newChildren;
		},
		isAncestor : function(haystack, needle){
			var haystack = this.getId(haystack);
			var needle = this.getId(needle);
			
			var ret = false;
			
			if( (haystack && needle) && (haystack['nodeType'] && needle['nodeType']) ){
				if (haystack.contains && haystack !== needle) {
                    ret = haystack.contains(needle);
                }
                else if (haystack.compareDocumentPosition) {
                    ret = !!(haystack.compareDocumentPosition(needle) & 16);
                }
			}else{}
			return ret;
		}
	};
	
})();
/**
 * ajax util
 */
var AJAX = (function(){
	function handleReadyState(o, callback){
		var poll = window.setInterval(
			function(){
				if(o && o.readyState === 4){
					window.clearInterval(poll);
					if(callback){
						callback(o);
					}
				}
			},50
		);
	};
	var getXHR = function(){
		var xhr,
			msxml = [
		                'MSXML2.XMLHTTP.5.0',
		                'MSXML2.XMLHTTP.4.0',
		                'MSXML2.XMLHTTP.3.0',
		                'MSXML2.XMLHTTP',
		                'Microsoft.XMLHTTP'
		                ];
;
		try{
			xhr = new XMLHttpRequest();
			getXHR = function(){
				return new XMLHttpRequest;
			}
		}catch(e){
			for(var i=0, len=msxml.length; i<len; i++){
				try{
					xhr = new ActiveXObject(msxml[i]);
					getXHR = function(){
						return new ActiveXObject(msxml[i]);
					}
					break;
				}catch(e){}
			}
		}
		return xhr;
	};
	
	return function(method, uri, callback, postData){
		var xhr = getXHR();
		xhr.open(method, uri, true);
		handleReadyState(xhr, callback);
		xhr.send(postData || null);
		return xhr;
	};
})();

var D = jav.dom, E = jav.ev;
	E.on = E.addEventHandler;
	G = D.getId ;

jav.namespase('effect');
/**
 * tab	accordion  
 */
jav.effect = (function(){
	
	return {
		tab : function(Obj){
			var oLis = D.getChildren(Obj.navId),oLisLen = oLis.length, aAs = [], aRelDivs = [];
			for(var i = 0; i<oLisLen; i++){
				var oA = D.getChildren(oLis[i])[0];
				aAs[aAs.length]=oA;
				aRelDivs[aRelDivs.length]=oA.rel;
			}
			
			//yui 中event.on的价值，渐渐理解了它 的源码，对多个元素统一事件类型，支持事件类型兼容性
			
			for(var i = 0; i<oLisLen; i++){
				aAs[i].onclick = function(){
					for(var i=0; i<oLisLen; i++){
						if(this.parentNode==oLis[i] && this.rel==aRelDivs[i]){
							oLis[i].className = 'cur';
							G(aRelDivs[i]).style.display = 'block';
						}else{
							oLis[i].className = '';
							G(aRelDivs[i]).style.display = 'none';
						}
					}
				};
			}
		}
	};
	
})();